package com.contest.interceptor;

import com.contest.constant.CommonsConstant;
import com.contest.entity.UserInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台管理员登录拦截
 */
public class ClientRequestInterceptor implements HandlerInterceptor {
    private ObjectMapper json = new ObjectMapper();
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute(CommonsConstant.CLIENT_LOGIN_SESSION);
        if (userInfo == null) {
//            Map<String,Object> res = new HashMap<String,Object>();
//            res.put("code", -1);
//            res.put("msg", "请登录!");
//            res.put("obj", null);
//            response.setCharacterEncoding("UTF-8");
//            response.setContentType("application/json; charset=utf-8");
//            PrintWriter out = response.getWriter();
//            out.print(json.writeValueAsString(res));
//            out.flush();
//            out.close();
            return true;
        } else {
            return true;
        }

    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
